package com.asynchfileupload.view;
/**  
* FileUploadListener.java - This class listenes to the file upload progress.  
* @author  Avinash Singh (349933)
* @version 1.0 
*/

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.ProgressListener;

public class FileUploadListener implements ProgressListener,AsynchFileUploadConstants{
	
	private String fileSessionID;
	private HttpServletRequest request;
	private long megaBytes = -1;
	private volatile long bytesRead = 0L, contentLength = 0L, item = 0L;   

	public FileUploadListener(){}
	
	public FileUploadListener(HttpServletRequest request,String fileSessionID) {
		this.request = request;		
		this.fileSessionID = fileSessionID;
	}
	
	/*
	 * This method updates the file read parameters and store the data in session.
	 * @see org.apache.commons.fileupload.ProgressListener#update(long, long, int)
	 */
	public void update(long aBytesRead, long aContentLength, int anItem) {
		long mBytes = aBytesRead / 1000000;
		
			this.bytesRead = aBytesRead;
			this.contentLength = aContentLength;
			this.item = anItem;
			this.megaBytes = mBytes;	  
			
			if(fileSessionID!=null)
				request.getSession().setAttribute(LISTENERID_PREFIX + fileSessionID, this);		
	}

	public long getBytesRead() {
		return bytesRead;
	}

	public long getContentLength() {
		return contentLength;
	}

	public long getItem() {
		return item;
	}

	public String getfileSessionID() {
		return fileSessionID;
	}

	public void setfileSessionID(String fileSessionID) {
		this.fileSessionID = fileSessionID;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
}